<template>
  <el-cascader
    :value="value"
    size="mini"
    :options="treeData"
    :props="props"
    @change="handleChange"
  />
</template>

<script>
import { deptGetDepartmentsApi } from '@/api/department'
import { transListToTreeData } from '@/utils'

export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      treeData: [],
      props: {
        label: 'name',
        value: 'id'
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    /**
     * 获取树形部门列表数据
     */
    async getDepartment() {
      this.treeData = transListToTreeData(await deptGetDepartmentsApi(), 0)
    },
    /**
     * 选中节点变化时触发
     * @param list 选中节点的 id 组成的数组
     */
    handleChange(list) {
      if (list.length) {
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>

<style scoped>

</style>
